//
//  MyViewheaderView.swift
//  MD5
//
//  Created by 郑东喜 on 2016/11/24.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

//代理
protocol MyViewheaderViewDelegate {
    ///跳转我的积分界面
    func jumpToAccout()
}

class MyViewheaderView: UIView {
    
    static let shared = MyViewheaderView()
    //设置代理监听按钮事件
    var delegate : MyViewheaderViewDelegate?
    
    //默认背景视图
    lazy var headerBgImg : UIImageView = {
        let v = UIImageView()
        v.contentMode = UIViewContentMode.scaleAspectFill
        v.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 180)
        v.image = UIImage.init(named: "jifenBg")
        return v
    }()
    
    // 标题
    var titleLabel : UILabel = {
        var lab = UILabel()
        lab.frame = CGRect(x: 0, y: 15, width: UIScreen.main.bounds.width, height: 44)
//        lab.backgroundColor = UIColor.gray
        lab.font = UIFont.boldSystemFont(ofSize: 16)
        lab.textAlignment = NSTextAlignment.center
        lab.textColor = UIColor.white
        lab.text = "我的"
        return lab
    }()
    
    //头像
    lazy var headImg : UIImageView = {
        let img = UIImageView()
        img.frame = CGRect(x: UIScreen.main.bounds.width / 2 - 25, y: 60, width: 50, height: 50)
        img.layer.cornerRadius = 25
        img.backgroundColor = UIColor.white
        img.layer.masksToBounds = true
        
        return img
    }()
    
    // 姓名
    var nickName : UILabel = {
        var lab = UILabel()
        lab.frame = CGRect(x: 0, y: 125, width: UIScreen.main.bounds.width, height: 44)
//        lab.backgroundColor = UIColor.gray
        lab.font = UIFont.boldSystemFont(ofSize: 16)
        lab.textAlignment = NSTextAlignment.center
        lab.textColor = UIColor.white

        return lab
    }()
    
    //默认背景视图
    lazy var donwView : UIView = {
        let v = UIView()
        v.contentMode = UIViewContentMode.scaleAspectFill
        v.frame = CGRect(x: 0, y: 180, width: UIScreen.main.bounds.width, height: 60)
        v.backgroundColor = UIColor.white
        return v
    }()
    
    
    //========================================================================
    //第一组第二行
    //========================================================================
    //前面的view
    lazy var oneViewFrontView : UIView = {
        let view = UIView()
        view.frame = CGRect(x: 0, y: 180, width: UIScreen.main.bounds.width, height: 60)
        view.backgroundColor = UIColor.white
        return view
    }()
    
    //后面的view
    lazy var oneViewBackView : UIView = {
        let view = UIView()
        view.frame = CGRect(x:  UIScreen.main.bounds.width * 0.6, y: 180, width: UIScreen.main.bounds.width * 0.4, height: 60)
//        view.backgroundColor = UIColor.purple
        return view
    }()
    
    //前面view中画一个居中的view，将控件塞进去
    lazy var frontMiddleView : UIView = {
        let view = UIView()
        
        view.backgroundColor = UIColor.white
        return view
    }()
    
    
    //========================================================================
    //开始塞控件
    //========================================================================
    //前面的view
    
    
    //积分图标
    lazy var jifenImg : UIImageView = {
        let img = UIImageView()
        img.image = UIImage.init(named: "money")
        return img
    }()
    
    //积分
    lazy var jifenLabel : UILabel = {
        let lab = UILabel()
        lab.text = "我的积分"
        lab.textAlignment = NSTextAlignment.center
        return lab
    }()
    
    
    //积分
    lazy var jifenText : UILabel = {
        let lab = UILabel()
        lab.text = "6789"
        lab.textColor = UIColor.green
        lab.textAlignment = NSTextAlignment.center
        return lab
    }()
    
    
    
    
    //========================================================================
    //第一组第二行 添加按钮单击事件(前面)
    //========================================================================
    lazy var frontBtn : UIButton = {
        let btn = UIButton()
//                btn.backgroundColor = UIColor.lightGray
        //frontBtnSEL
        btn.frame = CGRect(x: 0, y: 180, width: UIScreen.main.bounds.width * 0.6, height: 60)
        btn.addTarget(self, action: #selector(MyViewheaderView.frontBtnSEL), for: .touchUpInside)
        return btn
    }()
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension MyViewheaderView {
    func initView() -> Void {
        
        
        //第一组，第二行前面视图居中部分
        frontMiddleView.frame = CGRect(x: oneViewFrontView.frame.size.width - oneViewFrontView.frame.size.width * 0.825 , y: 0, width: oneViewFrontView.frame.size.width * 0.65, height: 60)

        
        //积分分数
        jifenLabel.frame = CGRect(x: SW / 2 - 60, y: 20, width: 80, height: 20)
        
        jifenImg.frame = CGRect(x: SW / 2 - 60 - 10 - 10, y: 20, width: 20, height: 20)
        
        jifenText.frame = CGRect(x: jifenLabel.RightX + 10, y: 20, width: 0, height: 20)
        jifenText.sizeToFit()

        print("模型的值",LoginModel.shared.personData as Any)
        
        //单例获取值
        if PersonInfoModel.shared.nickName != nil {
            self.nickName.text = PersonInfoModel.shared.nickName
        } else if PersonInfoModel.shared.personImg != nil {
            let imgURL = LoginModel.shared.personData?["img"] as? String

            
            //主线程 执行，防止阻塞
            
            DispatchQueue.main.async {
                DownImgTool.shared.downImgWithURLShowToImg(urlStr: imgURL!, imgView: self.headImg)
            }

        } else
        
        //程序启动赋值，本地加载。前提网络请求已缓存好
        if LoginModel.shared.personData?.count == nil {

            print("本地个人信息",localSave.object(forKey: personInfo) as? NSDictionary as Any)
            guard let ppData = localSave.object(forKey: personInfo) as? NSDictionary else {
                return
            }
            
            //昵称
            PersonInfoModel.shared.nickName = ppData["nickname"] as? String
            
            //昵称
            PersonInfoModel.shared.personImg = ppData["img"] as? String
            
            nickName.text = PersonInfoModel.shared.nickName
            
            let imgURL = PersonInfoModel.shared.personImg
            
            
            DispatchQueue.main.async {
                DownImgTool.shared.downImgWithURLShowToImg(urlStr: imgURL!, imgView: self.headImg)
            }

        }
        
        self.addSubview(headerBgImg)
        //添加标题
        self.addSubview(titleLabel)
        
        self.addSubview(headImg)
        
        self.addSubview(nickName)
        
        //添加积分、充值
        self.addSubview(donwView)
        
        
        
        //第一组第二行
        self.addSubview(oneViewFrontView)
        self.addSubview(oneViewBackView)
        
        //第一组第二行前面的控件
        self.oneViewFrontView.addSubview(frontMiddleView)
        //积分图片
        self.oneViewFrontView.addSubview(jifenImg)
        
        //积分分数
        self.oneViewFrontView.addSubview(jifenLabel)

        //积分文字
        self.oneViewFrontView.addSubview(jifenText)
        
        self.addSubview(frontBtn)

    }
}

// MARK:- 单击事件
extension MyViewheaderView {
    func clickImgSEL() -> Void {
    }
    
    //第一组第二行 前面按钮单机事件
    func frontBtnSEL() -> Void {
        print("前面的按钮单击事件")
        delegate?.jumpToAccout()
    }
    //第一组第二行 后面按钮单机事件
}
